package com.biqee.action.admin;

import java.util.List;

import com.biqee.action.base.BaseAction;
import com.biqee.exception.BusinessUnCheckedException;
import com.biqee.exception.ErrorCode;
import com.biqee.model.common.PageInfoObject;
import com.biqee.model.db.BqTuan;
import com.biqee.model.page.ItemVO;
import com.biqee.service.bq.BqQueryRelationService;
import com.biqee.service.bq.BqTuanService;
import com.biqee.service.bq.EstimateService;
import com.biqee.service.juhuasuan.JuHuaSuanParseService;
import com.biqee.util.StringUtil;
import com.biqee.util.UrlUtil;

public class TuanManagerAction extends BaseAction{

	private static final long serialVersionUID = -271061134288684156L;
	
	private BqTuanService bqTuanService;
	
	private PageInfoObject page = new PageInfoObject() ;
	
	private Integer tuanId ;
	
	private BqTuan bqTuan = new BqTuan();
	
 	private List<ItemVO> itemsList;
	
	
	private List<BqTuan> tuanList ;
	
	
	private BqQueryRelationService  bqQueryRelationService ;
	
	private JuHuaSuanParseService juHuaSuanParseService ;
	
	private EstimateService estimateService;
	
	
	
	public String update(){
		if(bqTuan==null||StringUtil.isEmpty(bqTuan.getJoinUrl())){
			throw new BusinessUnCheckedException(ErrorCode.ADMIN_TUAN_001);
		}
		if(bqTuan.getId()!=null){
			bqTuanService.search(bqTuan);
			estimateService.reEstimate(bqTuan.getId());	
		}else{
			boolean result = juHuaSuanParseService.parseByUrl(bqTuan.getJoinUrl(),true);
			if(result==false){
				throw new BusinessUnCheckedException(ErrorCode.JUHUASUAN_PARSE_ERROR);
			}else{
				bqTuan = bqTuanService.readBySiteItem(BqTuan.SITE_TAOBAO, UrlUtil.getParam(bqTuan.getJoinUrl(), "itemId"));
			}
			
		}
		tuanId= bqTuan.getId();
		return "directIndex";
	}
	

	
	
	public String index(){
		if(tuanId!=null){
			bqTuan = bqTuanService.getById(tuanId);
			page.addParam("tuanId", tuanId);
			itemsList = bqQueryRelationService.listRelationItemsByPage(page);
		}
		return "index";
	}
	
	
	


	public Integer getTuanId() {
		return tuanId;
	}




	public void setTuanId(Integer tuanId) {
		this.tuanId = tuanId;
	}

	public void setBqTuanService(BqTuanService bqTuanService) {
		this.bqTuanService = bqTuanService;
	}

	public List<BqTuan> getTuanList() {
		return tuanList;
	}

	public void setTuanList(List<BqTuan> tuanList) {
		this.tuanList = tuanList;
	}

	public PageInfoObject getPage() {
		return page;
	}


	public void setPage(PageInfoObject page) {
		this.page = page;
	}





	public BqTuan getBqTuan() {
		return bqTuan;
	}


	public Object getModel() {
		return bqTuan;
	}


	public void setBqTuan(BqTuan bqTuan) {
		this.bqTuan = bqTuan;
	}





	public void setItemsList(List<ItemVO> itemsList) {
		this.itemsList = itemsList;
	}





	public List<ItemVO> getItemsList() {
		return itemsList;
	}


	public void setBqQueryRelationService(
			BqQueryRelationService bqQueryRelationService) {
		this.bqQueryRelationService = bqQueryRelationService;
	}




	public void setJuHuaSuanParseService(JuHuaSuanParseService juHuaSuanParseService) {
		this.juHuaSuanParseService = juHuaSuanParseService;
	}




	public void setEstimateService(EstimateService estimateService) {
		this.estimateService = estimateService;
	}
	
	

}
